package com.wxprogram.smallprogram.controller;

import com.wxprogram.smallprogram.util.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.wxprogram.smallprogram.entity.UserStudent;
import com.wxprogram.smallprogram.entity.UserTeacher;
import com.wxprogram.smallprogram.service.UserInfoReqService;

@RestController
@RequestMapping("/users")
public class UserInfoReqController {
	
	@Autowired
	private UserInfoReqService userInfoReqService;
	/**
	 * 学生查看及修改个人信息 
	 */
	@GetMapping("/getStu")
	public UserStudent getStuInfo(@RequestParam(name="openID") String openID){
		
		return userInfoReqService.getStudentInfo(openID);
	}
	@GetMapping("/updateStu")
	public JSON UpdateStu(UserStudent userStudent){
		if (userInfoReqService.updateStudentInfo(userStudent) > 0){
			return JSON.ok("success");
		}else {
			return JSON.error("failure");
		}
	}
	//添加学生
	@GetMapping("/addStudent")
	public JSON addUserStudent(@RequestParam(name = "openID")String openID,@RequestParam(name = "degree")int degree){
		UserStudent userStudent = new UserStudent();
		userStudent.setStuopenID(openID);
		userStudent.setStudegree(degree);
		int count = userInfoReqService.insertStudent(userStudent);
		if(count>0){
			return JSON.ok("success");
		}else {
			return JSON.error("failure");
		}
	}
	
	/**
	 * 学长查看及修改个人信息
	 */
	@GetMapping("/getTeacherInfo")
	public UserTeacher getTeacherInfo(@RequestParam(name="openID") String openID){
		
		return userInfoReqService.getTeacherInfo(openID);
	}
	@GetMapping("/updateTeacher")
	public JSON UpdateTeacher(UserTeacher userTeacher){
		if (userInfoReqService.updateTeacherInfo(userTeacher) > 0){
			return JSON.ok("success");
		}else {
			return JSON.error("failure");
		}
		
	}

	//添加学长
	@GetMapping("/addTeacher")
	public JSON addUserTeacher(@RequestParam(name = "openID")String openID,@RequestParam(name = "degree")int degree){
		UserTeacher userTeacher = new UserTeacher();
		userTeacher.setTeacheropenID(openID);
		userTeacher.setTeacherdegree(degree);
		int count = userInfoReqService.insertTeacher(userTeacher);
		if(count>0){
			return JSON.ok("success");
		}else {
			return JSON.error("failure");
		}
	}

}
